﻿using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WH.Common.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class SwaggerEnumFilter : IDocumentFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="swaggerDoc"></param>
        /// <param name="context"></param>
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var dict = GetAllEnum();

            foreach (var item in swaggerDoc.Components.Schemas)
            //foreach (var item in swaggerDoc.Definitions)
            {
                var property = item.Value;
                var typeName = item.Key;
                if (property.Enum != null && property.Enum.Count > 0)
                {
                    var itemType = dict.ContainsKey(typeName) ? dict[typeName] : null;
                    var list = new List<OpenApiString>();
                    foreach (var val in property.Enum)
                    {
                        list.Add((OpenApiString)val);
                    }

                    if (itemType is not null)
                    {
                        property.Description += DescribeEnum(itemType, list);
                    }
                }
            }
        }
        private static Dictionary<string, Type> GetAllEnum()
        {
            var ass = Assembly.Load("WH.Model");
            var types = ass.GetTypes();
            var dict = new Dictionary<string, Type>();

            foreach (var item in types)
            {
                if (item.IsEnum)
                {
                    dict.Add(item.Name, item);
                }
            }
            return dict;
        }

        private static string DescribeEnum(Type type, List<OpenApiString> enums)
        {
            var enumDescriptions = new List<string>();
            foreach (var item in enums)
            {
                if (type == null)
                {
                    continue;
                }

                var value = Enum.Parse(type, item.Value.ToString());
                var desc = GetDescription(type, value);
                if (string.IsNullOrWhiteSpace(desc))
                {
                    enumDescriptions.Add($"{Convert.ToInt64(value)}:{item.Value}; ");
                }
                else
                {
                    enumDescriptions.Add($"{Convert.ToInt64(value)}:{item.Value},{desc}; ");
                }

            }
            return $"<br/>{Environment.NewLine}{string.Join("<br/>" + Environment.NewLine, enumDescriptions)}";
        }

        private static string GetDescription(Type t, object value)
        {
            foreach (var mInfo in t.GetMembers())
            {
                if (mInfo.Name == t.GetEnumName(value))
                {
                    foreach (var attr in Attribute.GetCustomAttributes(mInfo))
                    {
                        if (attr.GetType() == typeof(DescriptionAttribute))
                        {
                            return ((DescriptionAttribute)attr).Description;
                        }
                    }
                }
            }
            return string.Empty;
        }
    }
}